package com.songcha.library_common.ui.view.loading

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import com.songcha.library_common.util.FontUtil
import com.songcha.library_common.util.dp2px
import com.songcha.library_common.util.sp2px
import com.songcha.library_common.R
import kotlin.math.absoluteValue

class RingLoadingView  @kotlin.jvm.JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet?=null,
    defStyle: Int = 0,
    defRes: Int = 0
) : View(context, attrs, defStyle, defRes) {
    companion object{
        private val RING_LOADING_SIZE=100.dp2px()
        private var RING_WIDTH_RATIO=8
    }

    private val mCirclePaint=Paint()
    private val mArcPaint=Paint()
    private val mCircleColor=Color.parseColor("#38e6e6e6")
    private var mArcColor=Color.GREEN
    private var mCircleStrokeWidth=20f
    private var mProgress=0
    private var mProgressTextPaint=Paint()
    private var mProgressColor=Color.WHITE

    init {
        initView(context,attrs)
    }

    private fun initView(context: Context, attrs: AttributeSet?) {
        if (attrs != null) {
            val ta = context.obtainStyledAttributes(attrs, R.styleable.RingLoadingView)
            mArcColor=ta.getColor(R.styleable.RingLoadingView_ring_color,Color.GREEN)
            mProgressColor=ta.getColor(R.styleable.RingLoadingView_progress_color,Color.WHITE)
            ta.recycle()
        }

        mCirclePaint.isAntiAlias=true
        mCirclePaint.style=Paint.Style.STROKE
        mCirclePaint.color=mCircleColor

        mArcPaint.isAntiAlias=true
        mArcPaint.style=Paint.Style.STROKE
        mArcPaint.strokeCap=Paint.Cap.ROUND
        mArcPaint.color=mArcColor

        mProgressTextPaint.color=mProgressColor
        mProgressTextPaint.textSize=14f.sp2px()

    }

    fun setProgress(progress:Int){
        if(progress>100) mProgress=100
        if(progress<0) mProgress=0
        if(mProgress!=progress){
            mProgress=progress
            invalidate()
        }
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)

        val widthMode=MeasureSpec.getMode(widthMeasureSpec)
        val widthSize=MeasureSpec.getSize(widthMeasureSpec)
        val heightMode=MeasureSpec.getMode(heightMeasureSpec)
        val heightSize=MeasureSpec.getSize(heightMeasureSpec)

        if(widthMode==MeasureSpec.AT_MOST && heightMode==MeasureSpec.AT_MOST){
             setMeasuredDimension(RING_LOADING_SIZE,RING_LOADING_SIZE)
        } else if(widthMode!=MeasureSpec.UNSPECIFIED && heightMode!=MeasureSpec.UNSPECIFIED) {
            val size = Math.min(widthSize, heightSize)
            setMeasuredDimension(size,size)
        }

    }



    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        if(canvas==null)
            return

        if(mProgress==0) return

        mCircleStrokeWidth=(width/RING_WIDTH_RATIO).toFloat()
        mCirclePaint.strokeWidth=mCircleStrokeWidth
        mArcPaint.strokeWidth=mCircleStrokeWidth

        val rect=RectF(mCircleStrokeWidth/2,mCircleStrokeWidth/2,width.toFloat()-mCircleStrokeWidth/2,height.toFloat()-mCircleStrokeWidth/2)
        canvas.drawCircle((width/2).toFloat(),(height/2).toFloat(),(width/2)-mCircleStrokeWidth/2.toFloat(),mCirclePaint)
        canvas.drawArc(rect,-90f,(360*mProgress/100).toFloat(),false,mArcPaint)


        val str="$mProgress%"
        val textRect= FontUtil.getTextBounds(str,mProgressTextPaint.textSize,false)?:return
        val fontMetrics= FontUtil.getFontMetrics(mProgressTextPaint.textSize,false)?:return
        val textHeight=(fontMetrics.top.absoluteValue+fontMetrics.bottom)
        canvas.drawText(str,(width/2-textRect.width()/2).toFloat(),(height/2)+(textHeight/2-fontMetrics.bottom),mProgressTextPaint)

    }

}